package com.qingyun.student.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.qingyun.student.entity.StudentSubject;
import com.qingyun.student.mapper.StudentSubjectMapper;
import com.qingyun.student.service.IStudentSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 学生科目关联表 服务实现类
 * </p>
 *
 * @author your_name
 * @since 2025-01-01
 */
@Service
public class StudentSubjectServiceImpl extends ServiceImpl<StudentSubjectMapper, StudentSubject> implements IStudentSubjectService {

    @Autowired
    private StudentSubjectMapper studentSubjectMapper;

    @Override
    public List<String> getSubjectsByStudentId(Long studentId) {
        return studentSubjectMapper.getSubjectsByStudentId(studentId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStudentSubjects(Long studentId, List<String> subjects) {
        // 先删除原有的科目关联
        studentSubjectMapper.deleteByStudentId(studentId);
        
        // 如果有新的科目，则批量插入
        if (subjects != null && !subjects.isEmpty()) {
            return studentSubjectMapper.batchInsert(studentId, subjects) > 0;
        }
        
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByStudentId(Long studentId) {
        return studentSubjectMapper.deleteByStudentId(studentId) >= 0;
    }
}
